package com.lazyboyl.sharding.proxy.net.factory;

import com.lazyboyl.sharding.proxy.config.MySqlConfigEnum;
import com.lazyboyl.sharding.proxy.net.entity.ShardingLoginInfo;
import com.lazyboyl.sharding.proxy.net.frontend.FrontendConnection;
import com.lazyboyl.sharding.proxy.net.frontend.ServerQueryHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author linzf
 * @since 2021-06-30
 * 类描述： navicat的连接的核心工厂类生产类
 */
public class FrontConnectionFactory {

    private static final Logger logger = LoggerFactory.getLogger(FrontConnectionFactory.class);

    private ShardingLoginInfo shardingLoginInfo;

    public FrontConnectionFactory(ShardingLoginInfo shardingLoginInfo){
        this.shardingLoginInfo = shardingLoginInfo;
    }

    /**
     * MySql ThreadId Generator
     */
    private static final AtomicInteger ACCEPT_SEQ = new AtomicInteger(0);

    public FrontendConnection getConnection() {
        FrontendConnection connection = new FrontendConnection();
        connection.setShardingLoginInfo(shardingLoginInfo);
        connection.setQueryHandler(new ServerQueryHandler(connection));
        connection.setId(ACCEPT_SEQ.getAndIncrement());
        logger.info("connection Id="+connection.getId());
        connection.setCharset(MySqlConfigEnum.DEFAULT_CHARSET.getStrValue());
        return connection;
    }

}
